﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NS_DBOP;
using System.Data.SqlClient;
public partial class Flow_MyApply : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Bind();
            if (Request.QueryString["id"] != null)
            {
                send();
            }
            if (Request.QueryString["fdid"] != null)
            {
                del();
            }
        }
    }

    public void Bind()
    {
        int userid=Convert.ToInt32(new DBoperate().ExecuteScalar("select Employeeid from Employee where username='"+Session["UserName"]+"'"));
        DataSet ds = new DBoperate().ExecuteQuery("select * from FlowDoc where UserID="+userid,0,0,"table1");
        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();
        this.lblCount.Text="共"+ds.Tables[0].Rows.Count+"条记录";
    }

    public string getFlow(string id)
    {
        string flow = new DBoperate().ExecuteScalar("select Name from Flow where ID=" + Convert.ToInt32(id)).ToString();
        return flow;
    }

    public string getStep(string id)
    {
        string step = null;
        int end = Convert.ToInt32(new DBoperate().ExecuteScalar("select IsEnd from FlowDoc where ID=" + Convert.ToInt32(id)));
        if (end > 0)
        {
            step = "完成";
        }
        else
        {

            DataSet ds = new DBoperate().ExecuteQuery("select * from FlowPath where DocID=" + Convert.ToInt32(id), 0, 0, "action");
            //Response.Write("<script>alert('" + dr["IsApprove"].ToString() + "')</script>");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (Convert.ToInt32(dr["IsApprove"]) != 2)
                {
                    string sql = "select Name from FlowAction where ID in (select ActionId from FlowStep where Num=" + Convert.ToInt32(dr["Num"]) + " and FlowID=" + Convert.ToInt32(dr["FlowID"]) + ")";
                    step = new DBoperate().ExecuteScalar(sql).ToString();

                    break;
                }
            }
        }
        return step;
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }

    public void send()
    {

        int approve = Convert.ToInt32(new DBoperate().ExecuteScalar("select top 1 Num from Path where FlowID=" + Convert.ToInt32(Request.QueryString["flowid"]) + " order by Num desc"));
        int userid=Convert.ToInt32(new DBoperate().ExecuteScalar("select Employeeid from Employee where username='"+Session["UserName"].ToString()+"'"));
        int num = Convert.ToInt32(new DBoperate().ExecuteScalar("select Num from Path where UserID="+userid));
        DataSet ds = new DBoperate().ExecuteQuery("select * from Path where FlowID="+Convert.ToInt32(Request.QueryString["flowid"]), 0, 0, "path");
        int result = 0;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            if (Convert.ToInt32(dr["UserID"]) == userid)
            {
                string strSql = "insert into FlowPath (DocID,FlowID,StepID,UserID,IsApprove,Num,IsJoin) values(" + Convert.ToInt32(Request.QueryString["id"]) + "," +
                Convert.ToInt32(dr["FlowID"]) + "," + Convert.ToInt32(dr["ID"]) + "," + dr["UserID"].ToString() + "," + (approve - Convert.ToInt32(dr["Num"])) + "," + dr["Num"].ToString() + "," + dr["IsJoin"].ToString() + ")";
                result = new DBoperate().ExecuteUpdate(strSql);
                continue;
            }
            if (Convert.ToInt32(dr["Num"]) > num)
            {
                string strsql = "insert into FlowPath (DocID,FlowID,StepID,UserID,IsApprove,Num,IsJoin) values(" + Convert.ToInt32(Request.QueryString["id"]) + "," +
                    Convert.ToInt32(dr["FlowID"]) + "," + Convert.ToInt32(dr["ID"]) + "," + dr["UserID"].ToString() + "," + (approve - Convert.ToInt32(dr["Num"])) + "," + dr["Num"].ToString() + "," + dr["IsJoin"].ToString() + ")";
                result = new DBoperate().ExecuteUpdate(strsql);
            }
        }
        //修改FlowDoc表中IsSave字段值
        if (result > 0)
        {
            int rs = new DBoperate().ExecuteUpdate("update FlowDoc set IsSave=0 where FlowID=" + Convert.ToInt32(Request.QueryString["flowid"]) );
        }
        Bind();
    }

    public void del()
    {
        int result = new DBoperate().ExecuteUpdate("delete from FlowDoc where ID=" + Request.QueryString["fdid"]);
    }
}
